CMS / Coding / Permission Setting
Permissions
-
Admin side
Files
1. Download Source file click here
2. Extract the file
3. copy and paste the 'app' and 'resource' folder to the root of the project
Route
add routes to route/admin.php
use App\Http\Controllers\admin\RoleController; //for role Route::get('/role',[RoleController::class, 'index'])->name('admin.role'); Route::get('/role/form',[RoleController::class, 'form'])->name('admin.role.form'); Route::post('/role/save',[RoleController::class, 'save'])->name('admin.role.save'); Route::delete('/role/delete',[RoleController::class, 'delete'])->name('admin.role.delete'); Route::get('/role/status/{id}',[RoleController::class, 'status'])->name('admin.role.status'); Route::post('/role/priority', [RoleController::class, 'priority'])->name('admin.role.priority'); DB
admin_menu_activity_masters
CREATE TABLE `admin_menu_activity_masters` ( `id` bigint UNSIGNED NOT NULL, `activity_identifier` char(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `title` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `priority` int DEFAULT NULL, `deleted_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- -- Dumping data for table `admin_menu_activity_masters` -- INSERT INTO `admin_menu_activity_masters` (`id`, `activity_identifier`, `title`, `priority`, `deleted_at`, `created_at`, `updated_at`) VALUES (1, 'create', 'Create', 2, NULL, NULL, NULL), (2, 'edit', 'Edit', 3, NULL, NULL, NULL), (3, 'view', 'View', 1, NULL, NULL, NULL), (4, 'delete', 'Delete', 4, NULL, NULL, NULL), (5, 'status', 'Status', 5, NULL, NULL, NULL); -- -- Indexes for dumped tables -- -- -- Indexes for table `admin_menu_activity_masters` -- ALTER TABLE `admin_menu_activity_masters` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `admin_menu_activity_masters` -- ALTER TABLE `admin_menu_activity_masters` MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6; Menu Items
INSERT INTO `admin_menus` ( `title`, `parent_id`, `admin_access`, `icon`, `admin_route`, `identifier`, `query_str`, `custom_fields`, `status`, `priority`, `deleted_at`, `created_at`, `updated_at`) VALUES ( 'Category', 7, '0', 'fas fa-address-book', 'admin.master', 'category', '', '{\"grid\":{\"header\":[\n {\"title\":\"Title\",\"field\":\"title\",\"type\":\"text\",\"width\":\"100%\"} \n ],\n \"top_tools\":[{\"title\":\"Add New\",\"route\":\"admin.pages.form\",\"type\":\"link\"},\n {\"title\":\"List All\",\"route\":\"admin.pages\",\"type\":\"link\"}\n ],\n \"right_tools\":[{\"title\":\"Edit\",\"route\":\"admin.pages.form\",\"type\":\"link\"},\n {\"title\":\"Delete\",\"route\":\"admin.pages.delete\",\"type\":\"delete\"},\n {\"title\":\"Status\",\"route\":\"admin.pages.status\",\"type\":\"status\"}]},\n\"form\":{\"data\":[{\"title\":\"Title\",\"field\":\"title\",\"type\":\"text\",\"col\":\"12\"}\n \n \n \n],\n\"top_tools\":[{\"title\":\"Add New\",\"route\":\"admin.pages.form\",\"type\":\"link\"},\n {\"title\":\"List All\",\"route\":\"admin.pages\",\"type\":\"link\"}], \n \"bottom_tools\":[{\"title\":\"Save\",\"route\":\"admin.pages.form\",\"type\":\"submit\"},\n {\"title\":\"Cancel\",\"route\":\"admin.pages\",\"type\":\"link\"}]\n}}', '1', 1, NULL, NULL, NULL); 1.admin_route should be admin.master for all sections
2. title and identifier should be unique for each sections
Admin menu with activity
CREATE TABLE `admin_menu_activities` ( `id` bigint UNSIGNED NOT NULL, `menu_identifier` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `activity_identifier` char(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `admin_menu_id` int DEFAULT NULL, `title` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `icon` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `admin_route` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `position` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `type` char(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `status` enum('1','0') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '1', `priority` int DEFAULT NULL, `deleted_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- -- Dumping data for table `admin_menu_activities` -- INSERT INTO `admin_menu_activities` ( `menu_identifier`, `activity_identifier`, `admin_menu_id`, `title`, `icon`, `admin_route`, `position`, `type`, `status`, `priority`, `deleted_at`, `created_at`, `updated_at`) VALUES ( 'category', 'view', 70, NULL, NULL, NULL, NULL, NULL, '1', NULL, NULL, NULL, NULL), ( 'category', 'create', 70, NULL, NULL, NULL, NULL, NULL, '1', NULL, NULL, NULL, NULL), ( 'category', 'edit', 70, NULL, NULL, NULL, NULL, NULL, '1', NULL, NULL, NULL, NULL), ( 'category', 'delete', 70, NULL, NULL, NULL, NULL, NULL, '1', NULL, NULL, NULL, NULL); -- -- Indexes for dumped tables -- -- -- Indexes for table `admin_menu_activities` -- ALTER TABLE `admin_menu_activities` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `admin_menu_activities` -- ALTER TABLE `admin_menu_activities` MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=35; 1. menu_identifier should be the value of identifier in admin_menu
2. activity_identifier should be the value of identifier in admin_menu_activity_masters
3. admin_menu_id should be the id of corresponding menu item in admin_menu
Check permission
define a function in controller/admin/AdminBaseController.php
public function getUserActivity($menu_id, $role_id){ $userActivity=DB::table('admin_menu_activities')->join('role_menu_activities','role_menu_activities.activity_id','admin_menu_activities.id') ->where('role_menu_activities.role_id', $role_id) ->where('menu_identifier',$menu_id)->get(); $this->data['permissions']=[]; if(isset($userActivity)){ foreach($userActivity as $row){ $this->data['permissions'][$row->activity_identifier]=1; } } } call getUserActivity() in the controller function
public function index(Request $request) { $user=auth()->guard('admin')->user(); $this->getUserActivity($input['identifier'], $user->role_id); return view('admin.master.index',$this->data ); } public function view(Request $request, $id){ $user = auth()->guard('admin')->user(); $input=$request->all(); $this->getUserActivity($this->data['identifier'], $user->role_id); $this->data['formData']=TaskRepository::getOne($id); switch($this->data['formData']->status){ case 'New': unset($this->data['permissions']['service_center_allocation']); unset($this->data['permissions']['verification']); break; case 'InProgress': unset($this->data['permissions']['service_center_allocation']); unset($this->data['permissions']['verification']); break; case 'Pending': unset($this->data['permissions']['service_center_allocation']); unset($this->data['permissions']['verification']); break; case 'Service': unset($this->data['permissions']['verification']); break; } } in view
php if(in_array('create', $permissions)){?> Create New php } ?> php if(in_array('delete', $permissions)){?> Delete All php } ?> -
Same Route for different sections
Table :admin_menus
Table :admin_menu_activities
identifier and query_string parameter must be passed along with all url ( view, edit, delete, save, redirect after saving) In links
route('admin.'.$route.'.form',['identifier'=>request()->identifier, 'qry_str'=>>request()->qry_str]) in form
in redirection
return redirect()->route('admin.'.$this->data['route'].'.index',['identifier' =>$input['identifier', 'qry_str'=>>request()->qry_str]]);